#ifndef BRT_STREAM_CONVERT
#define BRT_STREAM_CONVERT

#include "Stream.h"

#define This (static_cast<T*>(this))

RETURNCODE E_READFILESIZE  = 210;
RETURNCODE E_WRITEFILESIZE = 211;

template< class T >
class StreamConvert :
	public Stream
{
	template< class M >
	int32 ReadValueMemory( M* value , uint32 count )
	{
		uint32 sizeRead = 0;
		uint32 readSize = count * sizeof(M);

		int32 code = This->Read( (byte*)value , readSize , sizeRead );

		if( code != R_SUCCEED )
			return code;

		if(readSize != sizeRead )
			return E_READFILESIZE;
		return R_SUCCEED;
	}

	template< class M >
	INT32 WriteValueMemory( M* value , uint32 count )
	{
		uint32 sizeWrite = 0;
		uint32 writeSize = count * sizeof(M);

		int32 code = This->Write( value , writeSize , sizeWrite );

		if(code != R_SUCCEED )
			return code;
		if( writeSize != sizeWrite )
			return E_WRITEFILESIZE;
		return R_SUCCEED;
	}
public:
	template< class POD > inline
	int32 ReadValue( POD& value )
	{
		uint32 readSize = sizeof(POD);
		uint32 sizeRead = 0;

		int32 code = This->Read( (byte*)&value , readSize , sizeRead );
		if( code != R_SUCCEED )
			return code;

		if(sizeRead != readSize )
		{
			return E_READFILESIZE;
		}

		return R_SUCCEED;
	}

	template< class POD > inline
	int32 WriteValue( const POD& value )
	{
		uint32 writeSize = sizeof(POD);
		uint32 sizeWrite = 0;

		int32 code = This->Write( (byte*)&value , writeSize , sizeWrite );

		if( code != R_SUCCEED )
			return code;

		if( writeSize != sizeWrite )
			return E_WRITEFILESIZE;
		return R_SUCCEED;
	}

	int32 ReadValue( cchar* value , uint32 count )
	{
		return ReadValueMemory( value , count );
	}

	int32 ReadValue( cwchar* value , int32 count )
	{
		return ReadValueMemory( value , count );
	}

	int32 WriteValue( cchar* value , uint32 count )
	{
		return WriteValueMemory( value , count );
	}

	int32 WriteValue( cwchar* value , uint32 count )
	{
		return WriteValueMemory( value , count );
	}
	
};


#undef This
#endif